package com.lsy.weizhi2.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.MyJoin;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.activity.NoteDetail;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.myutils.ManagerItem;
import com.lsy.weizhi2.utils.MyMethod;
import com.lsy.weizhi2.utils.MyOkHttpUtils;
import com.zhy.autolayout.utils.AutoUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by 嗨哟 on 2017/5/5.
 */

public class MyJoinAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<MyJoin.ListBean> list_join;
    private LayoutInflater layoutInflater;
    private boolean bottom;
    private int bottom_count,tag;//是否显示底部，底部显示文字

    public MyJoinAdapter(Context context, List<MyJoin.ListBean> list_join) {
        this.context = context;
        this.list_join = list_join;
        layoutInflater=LayoutInflater.from(context);
    }

    public void UpData(List<MyJoin.ListBean> list_join,int bottom_count,int tag){
        this.list_join = list_join;
        this.bottom_count=bottom_count;
        this.tag=tag;
        this.notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view=layoutInflater.inflate(R.layout.manager_item,viewGroup,false);
        View view_bottom = layoutInflater.inflate(R.layout.bottom_view,viewGroup,false);
        AutoUtils.autoSize(view);
        AutoUtils.autoSize(view_bottom);
        if(i== ManagerItem.Bottom){
            return new BottomHolder(view_bottom);
        }else{
            return new ManagerViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ManagerViewHolder){
            ((ManagerViewHolder) holder).position=position;
            if(list_join!=null&&list_join.size()!=0){
                ((ManagerViewHolder) holder).manager_name.setText("发布者:"+list_join.get(position).getOrder().getRealName());
                ((ManagerViewHolder) holder).manager_title.setText(list_join.get(position).getOrder().getTitle());
                ((ManagerViewHolder) holder).manager_time.setText(list_join.get(position).getOrder().getCreateTime());
                ((ManagerViewHolder) holder).manager_num.setText("已成交"+list_join.get(position).getOrder().getDealCount()+"次");
                switch (list_join.get(position).getOrder().getPriceMethod()){
                    case 0:
                        ((ManagerViewHolder) holder).manager_price.setText(list_join.get(position).getOrder().getPrice()+"/次");//服务单价
                        break;
                    case 1:
                        ((ManagerViewHolder) holder).manager_price.setText(list_join.get(position).getOrder().getPrice()+"/时");//服务单价
                        break;
                    case 2:
                        ((ManagerViewHolder) holder).manager_price.setText(list_join.get(position).getOrder().getPrice()+"/天");//服务单价
                        break;
                    case 3:
                        ((ManagerViewHolder) holder).manager_price.setText(list_join.get(position).getOrder().getPrice()+"/周");//服务单价
                        break;
                    case 4:
                        ((ManagerViewHolder) holder).manager_price.setText(list_join.get(position).getOrder().getPrice()+"/月");//服务单价
                        break;
                    case 5:
                        ((ManagerViewHolder) holder).manager_price.setText(list_join.get(position).getOrder().getPrice()+"/年");//服务单价
                        break;
                }
            }
        }else if(holder instanceof BottomHolder){
            if(bottom_count==1&&tag==0){
                ((BottomHolder) holder).bottom_tv.setText("松开加载");
            }else if(bottom_count==1&&tag==1){
                ((BottomHolder) holder).bottom_tv.setText("正在加载");
            }else if(bottom_count==1&&tag==2){
                ((BottomHolder) holder).bottom_tv.setText("到底了");
            }
        }

    }

    @Override
    public int getItemViewType(int position) {
        if(list_join!=null&&position==list_join.size()){
            return ManagerItem.Bottom;
        }else{
            return super.getItemViewType(position);
        }

    }

    @Override
    public int getItemCount() {
        return list_join==null?0+bottom_count:list_join.size()+bottom_count;
    }

    class ManagerViewHolder extends RecyclerView.ViewHolder{
        int position;
        RelativeLayout manager_item_view;
        TextView manager_title,manager_classify,manager_time,manager_price,manager_num,manager_name;
        public ManagerViewHolder(View itemView) {
            super(itemView);
            manager_item_view= (RelativeLayout) itemView.findViewById(R.id.manager_item_view);
            manager_title= (TextView) itemView.findViewById(R.id.manager_title);
            manager_classify= (TextView) itemView.findViewById(R.id.manager_classify);
            manager_time= (TextView) itemView.findViewById(R.id.manager_time);
            manager_price= (TextView) itemView.findViewById(R.id.manager_price);
            manager_num= (TextView) itemView.findViewById(R.id.manager_num);
            manager_name= (TextView) itemView.findViewById(R.id.manager_name);
            manager_item_view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Gson gson=new Gson();
                    if(list_join!=null&&list_join.get(position)!=null){
                        Log.i("","position======"+position);
                        String json=gson.toJson(list_join.get(position));
                        MyMethod.toActivity(context,NoteDetail.class,new String[]{"json","tag"},new String[]{json,ManagerItem.CollectWeiZhi+""});
                    }
                }
            });
        }
    }

    class BottomHolder extends RecyclerView.ViewHolder{
        TextView bottom_tv;
        public BottomHolder(View itemView) {
            super(itemView);
            bottom_tv= (TextView) itemView.findViewById(R.id.bottom_tv);
        }
    }
}
